package com.hzhc.controller.wxapp;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.entity.UserMember;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.SysUserSecurityService;
import com.hzhc.service.UserMemberService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.vo.UserMemberVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author dongcy
 * @date 2024/7/26 13:45
 * @description WxApiDriverController
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/app")
public class WxApiDriverController {

    private final UserMemberService userMemberService;

    private final SysUserSecurityService securityService;

    @Value("${sjRoleId}")
    private Long sjRoleId;

    /**
     *  小程序扫码 保存司机账号
     * @param request
     * @param data
     * @return
     */
    @PostMapping("/saveDriverMember")
    @SysTemLog(operModul = "基础信息-客户账号新增", operType = "新增", operDesc = "客户账号新增功能", modular = "PC")
    public ResultUtil<T> saveDriverMember(HttpServletRequest request, @RequestBody JSONObject data) {
        UserMemberVo vo = data.to(UserMemberVo.class);

        LambdaQueryWrapper<UserMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserMember::getUsername,vo.getUsername())
                .eq(UserMember::getDisable,0);

        UserMember one = userMemberService.getOne(wrapper);
        boolean flag = true;
        if (one==null){//没有则新增
            String field = "username";
            String username = data.getString("username");
            Boolean bool1 = userMemberService.checkUserMemberByField(field, username,null);
            if (bool1) {
                return ResultUtil.build(ResultUtilCodeEnum.LOGIN_NAME_AlREAD_EXIST);
            }

            String field1 = "telephone";
            String telephone = data.getString("telephone");
            Boolean bool2 = userMemberService.checkUserMemberByField(field1, telephone, null);
            if (bool2) {
                return ResultUtil.build(ResultUtilCodeEnum.MOBILE_AlREAD_EXIST);
            }
//            UserDto user = TokenUtil.getUser(request);
//            vo.setUserId(user.getId());
            //客户账号 密码java正则校验  开始
            ResultUtil<T> rel = securityService.checkPasswordLevel(vo.getPassword());
            if (rel.getCode()==400){
                return rel;
            }
            //客户账号 密码java正则校验  结束
            vo.setFleet(vo.getCompanyId().toString());//车队id
            vo.setCompanyId(null);//公司id
            vo.setSysWxRoleId(sjRoleId);
            vo.setInoutflag(0);//扫码默认外部
            boolean bool  = userMemberService.saveUserMember(vo);
            flag = false;
        }else{//有则更新信息+加入新车队
            userMemberService.updateDriver(vo);
            flag = false;
        }

        if(!flag){
            return ResultUtil.ok();
        }else{
            return ResultUtil.fail();
        }
    }


    /**
     * 重置密码
     * @param userMember
     * @return
     */
    @PostMapping("/updatePwd")
    @SysTemLog(operModul = "客户账号重置密码", operType = "重置密码", operDesc = "客户账号重置密码功能", modular="APP")
    public ResultUtil<T> updatePwd(@RequestBody UserMember userMember) {
        ResultUtil<T> resultUtil = userMemberService.updatePwd(userMember);
        return resultUtil;
    }
}
